package com.iespring.service.poromotion;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iespring.bean.admin.promotion.bo.MallTopicBo;
import com.iespring.bean.admin.promotion.po.MallTopic;
import com.iespring.bean.admin.promotion.po.MallTopicExample;
import com.iespring.bean.admin.promotion.vo.MallTopicVo;
import com.iespring.bean.common.BasePageInfo;
import com.iespring.bean.common.CommonData;
import com.iespring.mapper.admin.MallTopicMapper;
import com.iespring.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author:ZhangShuPeng
 * @Date:2022/12/29/21:13
 * @Description:
 */
@Service
public class PromotionTopicServiceImpl implements PromotionTopicService {

    @Autowired
    MallTopicMapper mallTopicMapper;


    @Override
    public CommonData<MallTopic> list(BasePageInfo info, String title, String subTitle) {
        PageHelper.startPage(info.getPage(), info.getLimit());

        MallTopicExample mallTopicExample = new MallTopicExample();
        mallTopicExample.setOrderByClause(info.getSort() + " " + info.getOrder());
        MallTopicExample.Criteria criteria = mallTopicExample.createCriteria();

        criteria.andDeletedEqualTo(false);

        if (!StrUtil.isEmpty(title)) {
            criteria.andTitleLike("%" + title + "%");
        }
        if (!StrUtil.isEmpty(subTitle)) {
            criteria.andSubtitleLike("%" + subTitle + "%");
        }

        List<MallTopic> mallTopics = mallTopicMapper.selectByExample(mallTopicExample);

        PageInfo<MallTopic> mallTopicPageInfo = new PageInfo<>(mallTopics);
//        int pageSize = mallTopicPageInfo.getPageSize();
//        int pages = mallTopicPageInfo.getPages();
//        int pageNum = mallTopicPageInfo.getPageNum();
//        long total = mallTopicPageInfo.getTotal();
//        List<MallTopic> list = mallTopicPageInfo.getList();
//
//        CommonData<MallTopic> data = new CommonData<MallTopic>((int) total, pages, pageSize, pageNum, list);

        return CommonData.data(mallTopicPageInfo);
    }

    @Override
    public MallTopicVo create(MallTopicBo mallTopicBo) {

        String title = mallTopicBo.getTitle();
        String subTitle = mallTopicBo.getSubTitle();
        String content = mallTopicBo.getContent();
        BigDecimal price = mallTopicBo.getPrice();
        String picUrl = mallTopicBo.getPicUrl();
        String readCount = mallTopicBo.getReadCount();
        String[] goods = mallTopicBo.getGoods();

        MallTopic mallTopic = new MallTopic();

        mallTopic.setTitle(title);
        mallTopic.setSubtitle(subTitle);
        mallTopic.setContent(content);
        mallTopic.setPrice(price);
        mallTopic.setPicUrl(picUrl);
        mallTopic.setReadCount(readCount);
        mallTopic.setAddTime(new Date());
        mallTopic.setUpdateTime(new Date());
        mallTopic.setGoods(goods);

        MallTopicVo mallTopicVo = new MallTopicVo();

        mallTopicVo.setAddTime(mallTopic.getAddTime());
        mallTopicVo.setContent(mallTopic.getContent());
        mallTopicVo.setGoods(mallTopic.getGoods());
        mallTopicVo.setId(mallTopic.getId());
        mallTopicVo.setPrice(mallTopic.getPrice());
        mallTopicVo.setPicUrl(mallTopic.getPicUrl());
        mallTopicVo.setReadCount(mallTopic.getReadCount());
        mallTopicVo.setSubtitle(mallTopic.getSubtitle());
        mallTopicVo.setTitle(mallTopic.getTitle());
        mallTopicVo.setUpdateTime(mallTopic.getUpdateTime());


        mallTopicMapper.insertSelective(mallTopic);

        return mallTopicVo;
    }

    @Override
    public MallTopic read(MallTopic mallTopic) {

        mallTopic.setUpdateTime(new Date());

        mallTopicMapper.updateByPrimaryKeySelective(mallTopic);

        return mallTopic;
    }

    @Override
    public int delete(MallTopic mallTopic) {

        mallTopic.setDeleted(true);
        int rows = mallTopicMapper.updateByPrimaryKeySelective(mallTopic);
        return rows;
    }

    @Override
    public int batchDelete(Integer[] integers) {
        int rows = 0;
        for (Integer integer : integers) {
//            MallTopic mallTopic = new MallTopic();
//            mallTopic.setId(integer);
            MallTopic mallTopic = mallTopicMapper.selectByPrimaryKey(integer);
            mallTopic.setDeleted(true);
            mallTopicMapper.updateByPrimaryKeySelective(mallTopic);
            rows++;
        }
        return rows;
    }
}
